package cn.px.rest.controller.store;

import cn.px.common.bean.AbstractPageResultBean;
import cn.px.common.util.BeanConverterUtil;
import cn.px.common.util.R;
import cn.px.common.validation.group.AddGroup;
import cn.px.common.validation.group.UpdateGroup;
import cn.px.dao.dto.store.StoreMonthReportDTO;
import cn.px.dao.entity.store.Store;
import cn.px.dao.entity.store.StoreMonthReport;
import cn.px.dao.util.ExcelUtil;
import cn.px.dao.vo.export.store.ExportStoreMonthReportVO;
import cn.px.rest.log.annotation.SysLog;
import cn.px.service.store.StoreMonthReportService;
import cn.px.service.upms.SysDeptService;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @Description 仓库月报表信息 控制器
 * @author 品讯科技
 * @date 2024-08
 * @Version Copyright (c) 2019,北京乾元坤和科技有限公司 All rights reserved.
 */
@RestController
@AllArgsConstructor
@RequestMapping("/store/month/reports")
public class StoreMonthReportController {

  private final StoreMonthReportService storeMonthReportService;
  private final SysDeptService sysDeptService;
  /**
   * 通过id查询单条记录
   *
   * @param id 主键
   * @return R
   */
  @GetMapping("/{id}")
  public R<Store> getById(@PathVariable("id") String id){
      return new R(storeMonthReportService.getModelById(id));
  }

  /**
   * 新增记录
   *
   * @param storeDTO 仓库月报表信息DTO
   * @return R
   */
  @SysLog("新增仓库月报表信息")
  @PostMapping
  @PreAuthorize("@pms.hasPermission('store-stores-add')")
  public R save(@Validated({AddGroup.class}) @RequestBody StoreMonthReportDTO storeDTO) {
      return new R<>(storeMonthReportService.saveModelByDTO(storeDTO));
  }

  /**
   * 修改记录
   *
   * @param storeDTO 仓库月报表信息DTO
   * @return R
   */
  @SysLog("修改仓库月报表信息")
  @PutMapping("/{id}")
  @PreAuthorize("@pms.hasPermission('store-stores-edit')")
  public R update(@PathVariable("id") String id, @Validated({UpdateGroup.class}) @RequestBody StoreMonthReportDTO storeDTO) {
      storeMonthReportService.modModelByDTO(storeDTO);
      return new R<>();
  }

  /**
   * 通过id删除一条记录
   *
   * @param id 主键
   * @return R
   */
  @SysLog("删除仓库月报表信息")
  @DeleteMapping("/{id}")
  @PreAuthorize("@pms.hasPermission('store-stores-del')")
  public R removeById(@PathVariable String id){
    storeMonthReportService.deleteByPrimaryKey(id);
    return new R<>();
  }

  /**
   * 获取分页
   *
   * @param pageNum 当前页码
   * @param pageSize 每页条数
   * @param storeDTO 仓库月报表信息DTO
   * @return R
   */
  @GetMapping("/page")
  public R<AbstractPageResultBean<StoreMonthReport>> query(StoreMonthReportDTO storeDTO, @RequestParam(defaultValue = "1") int pageNum, @RequestParam(defaultValue = "20") int pageSize) {
      AbstractPageResultBean<StoreMonthReport> vo = storeMonthReportService.selectPageList(storeDTO, pageNum, pageSize);
      return new R<>(vo);
  }

  /**
   * 获取列表
   *
   * @param storeDTO 仓库月报表信息DTO
   * @return R
   */
  @GetMapping("")
  public R query(StoreMonthReportDTO storeDTO) {
      return new R<>(storeMonthReportService.getModelListByDTO(storeDTO));
  }

  /**
     * 仓库月报表信息导出
     *
     * @param storeDTO 仓库月报表信息DTO
     * @return R
     */
  @GetMapping("/export")
  @SysLog("仓库月报表导出")
  @PreAuthorize("@pms.hasPermission('store-stores-export')")
  public void export(HttpServletResponse response, StoreMonthReportDTO storeDTO) {
    List<StoreMonthReport> list = storeMonthReportService.getModelListByDTO(storeDTO);
    String projectName = sysDeptService.selectById(storeDTO.getUseProject()).getName();
    String fileName = projectName + "-" + storeDTO.getYear()  + "-"+ storeDTO.getMonth() + "-仓库汇总报表";
    ExcelUtil.exportResponseDict(response, ExportStoreMonthReportVO.class, BeanConverterUtil.copyListProperties(list, ExportStoreMonthReportVO.class), fileName);
  }
}
